﻿using System;
using System.Collections.Generic;
using System.Linq;

using Fuse8.DomainFramework.Common;
using Fuse8.DomainFramework.Common.Locator;

using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.ContentManagementFramework.Domain.Commands.CommandParameters;

namespace Fuse8.ContentManagementFramework.BizFacade.Services
{
    public class CmsEntityAnnotationService : CmsBusinessService<CmsEntityAnnotation>
    {
        public virtual IEnumerable<CmsEntityAnnotation> GetForEntity(int id, string entityTypeParameter)
        {
            var command = DependencyServiceLocator.Current.GetInstance<CmsEntityAnnotationGetForEntityCommand>();

            var commandParameter = new CmsEntityAnnotationGetForEntityCommandParameter
            {
                EntityId = id,
                EntityTypeParameter = entityTypeParameter
            };

            command.ConfigureCommand(commandParameter);

            return Find(command);
        }

        public virtual void SaveForEntity(int entityId, string entityTypeParameter, IEnumerable<CmsEntityAnnotation> annotations, IEnumerable<int> annotationsForDelete = null, IEnumerable<int> commentsForDelete = null)
        {
            Guard.ArgumentNotEmpty(entityTypeParameter, "entityTypeParameter");

            var command = DependencyServiceLocator.Current.GetInstance<CmsEntityAnnotationSaveForEntityCommand>();

            var commandParameter = new CmsEntityAnnotationSaveForEntityCommandParameter
            {
                EntityId = entityId,
                EntityTypeParameter = entityTypeParameter,
                Annotations = annotations,
                AnnotationsForDelete = annotationsForDelete,
                CommentsForDelete = commentsForDelete
            };

            command.ConfigureCommand(commandParameter);

            TrackingEntityObjectMediator.TrackEntity(commandParameter);

            commandParameter.LastUpdatedBy = commandParameter.CreatedBy;
            commandParameter.DateLastUpdated = commandParameter.DateCreated;

            Execute(command);
        }
    }
}
